package cn.player.act;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.RelativeLayout;

import org.json.JSONObject;


import java.util.Map;

import cn.player.R;
import cntv.player.core.util.SprefUtils;
import cntv.player.engine.Const;
import cn.player.media.entity.BaseUrl;
import cn.player.media.util.ParseUtil;

public class LaunchActivity extends BaseActivity {
	
	private static final int MSG_LAUNCHER_SUCCESS = 0;
	private static final int TIME_DELAY = 3000;
	private long startTime;
	private RelativeLayout mRlRoot;
	
	Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case MSG_LAUNCHER_SUCCESS:
				Log.i("zl", "进入总时长= " + (System.currentTimeMillis() - startTime));
				Intent intent = new Intent(LaunchActivity.this,MainActivity.class);
				startActivity(intent);
				overridePendingTransition(R.anim.base_push_right_in,R.anim.base_fade_out);
				LaunchActivity.this.finish();
				break;

			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.launcher_view);
		
		findViews();

		startTime = System.currentTimeMillis();
		initJsonData(Const.BASE_PATH);
		//for test start
//		mHandler.sendEmptyMessageDelayed(LAUNCHER_SUCCESS, 1500);
		//for test end
	}

	private void findViews() {
		// TODO Auto-generated method stub
		mRlRoot = (RelativeLayout) findViewById(R.id.rl_launch_root); 
		
	}
	
	

	@Override
	protected void initDataSuccess(String url, JSONObject response) {
		// TODO Auto-generated method stub
		super.initDataSuccess(url, response);
		try {
			if (url.equals(Const.BASE_PATH)) {
				if(response != null){
					SprefUtils.getInstance(mContext).putString(Const.SHAR_PREF_KEY_CONFIG,response.toString());
					/*List<BaseUrl> baseUrls = ParseUtil.parseDataToCollection(response, "data", BaseUrl.class);
					app.setBaseUrls(BaseUrl.convertListToMap(baseUrls));*/
					
					Map<Object, BaseUrl> baseUrls = ParseUtil.parseDataToMap(response, "data", "title", BaseUrl.class);
					app.setBaseUrls(baseUrls);
				}
				//for test start
				Log.i("zl", "访问数据总时长= " + (System.currentTimeMillis() - startTime));
				mHandler.sendEmptyMessageDelayed(MSG_LAUNCHER_SUCCESS, TIME_DELAY);
				//for test end
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	protected void initDataFailed(String url, int errorNo, String strMsg) {
		// TODO Auto-generated method stub
		super.initDataFailed(url, errorNo, strMsg);
		Log.i("zl","errorNo = " + errorNo + ",strMsg = " + strMsg);
		if(app.getBaseUrls() == null){
			launcherError();
		}else{
//			mHandler.sendEmptyMessageDelayed(MSG_LAUNCHER_SUCCESS, TIME_DELAY);
		}
	}
	
	/**
	 * 弹框提示播放错误
	 */
	private void launcherError() {
		// TODO Auto-generated method stub
		new AlertDialog.Builder(mContext).setTitle("提示").setMessage("服务器暂时无法连接,请稍后重试!")
		.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.dismiss();
				LaunchActivity.this.finish();
			}
		}).show();
	} 
	
	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

}
